package com.py.gs.businese.unionbattletest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class UnionBattleTest2 {

	static AtomicInteger finishThread = new AtomicInteger(0);
	static ExecutorService executor = Executors.newFixedThreadPool(4);
	static Object obj = new Object();
	public static void main(String[] args) {
		int n = 1000;
		AtomicInteger totalBattleCount = new AtomicInteger(0);
		long begin = System.currentTimeMillis();
		finishThread.set(n);
		for (int i = 0; i < n; i++) {
			executor.execute(()->{
				UnionBattleService3 service = new UnionBattleService3(500, 50, 10000);
				service.battle();
				totalBattleCount.addAndGet(service.battleCount);
//				System.out.println("单次大臣战斗次数:" + service.battleCount);
				if(finishThread.decrementAndGet() == 0) {
					synchronized (obj) {
						obj.notify();
					}
				}
			});
		}

		synchronized (obj) {
			try {
//				System.out.println("1");
				obj.wait();
//				System.out.println("2");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		long end = System.currentTimeMillis();
		long costTime = end - begin;
		System.out.println(n + "次总耗时:" + costTime);
		System.out.println(n + "次总对战次数:" + totalBattleCount);
		System.exit(0);
	}
}
